import { getToken, setToken } from '@/token'
import { getQueryVariable, isNullOrEmpty } from '@/utils'
import router from '@/router'
import store from '@/store'

const whiteList = ['Login', 'AuthBack']

router.beforeEach((to, from, next) => {
  if (!whiteList.includes(to.name)) {
    const otherToken = getQueryVariable('token') || to.query?.token

    if (otherToken) {
      store.commit('systemStore/SET_SHOW_LEFT_BAR', false)
      setToken(otherToken)
    } else {
      store.commit('systemStore/SET_SHOW_LEFT_BAR', true)
    }

    const token = getToken()
    if (isNullOrEmpty(token)) {
      next({ name: 'Login' })
      return false
    }
  }
  next()
})
